<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>医院智能客服系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
            color: #333;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
        }
        header {
            background-color: #1e88e5;
            color: white;
            padding: 20px;
            text-align: center;
            border-radius: 5px 5px 0 0;
        }
        .chat-container {
            background-color: white;
            border-radius: 0 0 5px 5px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
            margin-bottom: 20px;
        }
        .chat-box {
            height: 400px;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 15px;
            border-radius: 5px;
        }
        .message {
            margin-bottom: 15px;
            padding: 10px 15px;
            border-radius: 18px;
            max-width: 70%;
            word-wrap: break-word;
        }
        .user-message {
            background-color: #e3f2fd;
            margin-left: auto;
            border-bottom-right-radius: 0;
        }
        .bot-message {
            background-color: #f1f1f1;
            margin-right: auto;
            border-bottom-left-radius: 0;
        }
        .input-area {
            display: flex;
        }
        #question {
            flex-grow: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        #ask-btn {
            padding: 10px 20px;
            background-color: #1e88e5;
            color: white;
            border: none;
            border-radius: 5px;
            margin-left: 10px;
            cursor: pointer;
            font-size: 16px;
        }
        #ask-btn:hover {
            background-color: #1565c0;
        }
        .nav {
            display: flex;
            margin-bottom: 20px;
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .nav a {
            padding: 15px 20px;
            text-decoration: none;
            color: #333;
            font-weight: bold;
        }
        .nav a:hover {
            background-color: #f1f1f1;
        }
        .nav a.active {
            background-color: #1e88e5;
            color: white;
        }
        .info-card {
            background-color: white;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #1e88e5;
            color: white;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"], textarea, select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        .btn {
            padding: 10px 20px;
            background-color: #1e88e5;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        .btn:hover {
            background-color: #1565c0;
        }
        .btn-danger {
            background-color: #e53935;
        }
        .btn-danger:hover {
            background-color: #c62828;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>医院智能客服系统</h1>
            <p>您好，我是智能医疗助手，请问有什么可以帮您？</p>
        </header>

        <div class="nav">
            <a href="/" class="active">智能问答</a>
            <a href="/departments">科室信息</a>
            <a href="/doctors">医生信息</a>
            <a href="/knowledge">知识库管理</a>
            <a href="/records">咨询记录</a>
        </div>

        <div class="chat-container">
            <div class="chat-box" id="chat-box">
                <div class="message bot-message">
                    您好！我是医院智能客服，可以为您提供以下服务：<br><br>
                    1. 医院基本信息查询（上班时间、科室位置等）<br>
                    2. 预约挂号指导<br>
                    3. 医保政策咨询<br>
                    4. 体检相关问题<br>
                    5. 医生信息查询<br><br>
                    请问有什么可以帮您？
                </div>
            </div>

            <div class="input-area">
                <input type="text" id="question" placeholder="请输入您的问题..." autofocus>
                <button id="ask-btn">发送</button>
            </div>
        </div>

        <div class="info-card">
            <h2>常见问题</h2>
            <ul>
                <li>医院上班时间是怎样的？</li>
                <li>如何预约挂号？</li>
                <li>儿科在几楼？</li>
                <li>体检需要空腹吗？</li>
                <li>医保报销比例是多少？</li>
            </ul>
        </div>
    </div>

    <script>
        document.getElementById('ask-btn').addEventListener('click', askQuestion);
        document.getElementById('question').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                askQuestion();
            }
        });

        function askQuestion() {
            const question = document.getElementById('question').value.trim();
            if (!question) return;

            // 添加用户消息到聊天框
            const chatBox = document.getElementById('chat-box');
            const userMessage = document.createElement('div');
            userMessage.className = 'message user-message';
            userMessage.textContent = question;
            chatBox.appendChild(userMessage);

            // 清空输入框
            document.getElementById('question').value = '';

            // 显示"正在思考"消息
            const thinkingMessage = document.createElement('div');
            thinkingMessage.className = 'message bot-message';
            thinkingMessage.textContent = '正在思考...';
            chatBox.appendChild(thinkingMessage);

            // 滚动到底部
            chatBox.scrollTop = chatBox.scrollHeight;

            // 发送请求到服务器
            fetch('/ask', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `question=${encodeURIComponent(question)}&user_id=user123`
            })
            .then(response => response.json())
            .then(data => {
                // 移除"正在思考"消息
                chatBox.removeChild(thinkingMessage);

                // 添加机器人回复
                const botMessage = document.createElement('div');
                botMessage.className = 'message bot-message';
                botMessage.innerHTML = data.answer.replace(/\n/g, '<br>') +
                    `<br><br><small>来源: ${data.source}</small>`;
                chatBox.appendChild(botMessage);

                // 滚动到底部
                chatBox.scrollTop = chatBox.scrollHeight;
            })
            .catch(error => {
                console.error('Error:', error);
                chatBox.removeChild(thinkingMessage);

                const errorMessage = document.createElement('div');
                errorMessage.className = 'message bot-message';
                errorMessage.textContent = '抱歉，服务暂时不可用，请稍后再试。';
                chatBox.appendChild(errorMessage);

                chatBox.scrollTop = chatBox.scrollHeight;
            });
        }
    </script>
</body>
</html>